package com.mxx.qh.network;

import android.util.Log;

import com.mxx.qh.App;
import com.mxx.qh.SPUtils;
import com.mxx.qh.config.ConstantsConfig;

import java.io.IOException;
import java.net.CookieManager;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import okhttp3.Headers;
import okhttp3.ResponseBody;
import retrofit2.Response;

public abstract class StringObserver implements Observer<Response<ResponseBody>> {


    @Override
    public void onSubscribe(@NonNull Disposable d) {
        Log.i("TAG", "");
    }

    @Override
    public void onNext(Response<ResponseBody> response) {
        //crsfttoken
        if (response.isSuccessful()) {
            if (response.code() == 200) {
                ResponseBody body = response.body();
                if (body == null) {
                    onFailed(response.code(), "数据为空");
                    return;
                }
                String json = null;
                try {
                    json = body.string().trim();
                } catch (IOException e) {
                    e.printStackTrace();
                    onFailed(response.code(), "解析异常");
                }
                onSuccess(json);
            } else {
                onFailed(response.code(), response.message());
            }
        } else {
            okhttp3.Response raw = response.raw();
            onFailed(response.code(), raw.message());
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        onFailed(null, e.getMessage());
    }

    @Override
    public void onComplete() {
        Log.i("TAG", "");
    }

    protected abstract void onSuccess(String originalJson);

    protected abstract void onFailed(Integer code, String msg);
}
